package org.apple.console;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Toolkit;
import java.util.Properties;

import javax.swing.JPanel;

import org.apple.data.Position;
import org.apple.data.SensorReading;
import org.apple.util.Configure;


class HarnessFrame extends Frame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1891622698222933049L;
	
	protected GraphPanel airTempratureGraphPanel;
	protected GraphPanel seaTempratureGraphPanel;
	protected GraphPanel windSpeedGraphPanel;
	private JPanel mainPanel = new JPanel();
	private Panel harnessPanel = new Panel();
	BuoyImagePanel imgPanel = null;
	
	public HarnessFrame() {
		image = Toolkit.getDefaultToolkit().getImage("buoy.jpg");

		mainPanel.setLayout(new GridLayout(1,2));
		imgPanel = new BuoyImagePanel(image);
		mainPanel.add(imgPanel);
		
		
		harnessPanel.setBackground(Color.black);
		harnessPanel.setLayout(new BorderLayout());

		JPanel visualMsgPanel = new JPanel();
		
		initVisualMsgPanel(visualMsgPanel);
		visualMsgPanel.setBounds(250, 0, 500, 500);
		
		harnessPanel.add(visualMsgPanel);

		mainPanel.add(harnessPanel);
		add(mainPanel);
		//add("East", harnessPanel);
	}
	
	protected GraphPanel firstPanel = null;
	
	public void receiveData(int type, String data) {
		switch(type) {
		case SensorReading.AIRTEMPRATURE:
			if (this.airTempratureGraphPanel != null){
				this.airTempratureGraphPanel.receiveData(data);
			}
			break;
		case SensorReading.SEATEMPRATURE:
			if (this.seaTempratureGraphPanel != null){
				this.seaTempratureGraphPanel.receiveData(data);
			}
			break;
		case SensorReading.SPEED:
			if (this.windSpeedGraphPanel != null){
				this.windSpeedGraphPanel.receiveData(data);
			}
			break;
		case Position.POSITION:	
			if (this.firstPanel != null)
				firstPanel.appendPositionInfo(data);
			break;
		default:
			throw new RuntimeException();
		}
		harnessPanel.repaint();
		
	}
	
	public void initVisualMsgPanel(JPanel visualMsgPanel) {
		Properties prop = Configure.instance();
		int visualPanelCount = 0;
		if ("true".equals(prop.getProperty("collectAirTemperature")))
			visualPanelCount ++;
		if ("true".equals(prop.getProperty("collectSeaTemperature")))
			visualPanelCount ++;
		if ("true".equals(prop.getProperty("collectWindSpeed")))
			visualPanelCount ++;
		
		visualMsgPanel.setLayout(new GridLayout(visualPanelCount, 1));///need to replace 4.
		if ("true".equals(prop.getProperty("collectAirTemperature"))) {
			GraphPanel panel = new GraphPanel("Air Temperature");
			visualMsgPanel.add(panel);
			airTempratureGraphPanel = panel;
			if (firstPanel == null)
				firstPanel = airTempratureGraphPanel;
			System.out.println("Configure: [HarnessFrame -> visual message panel]"
					+ " AirTempratureSensorMonitor Panel");
		}
		if ("true".equals(prop.getProperty("collectSeaTemperature"))) {
			GraphPanel panel = new GraphPanel("Sea Temperature");
			visualMsgPanel.add(panel);
			seaTempratureGraphPanel = panel;
			if (firstPanel == null)
				firstPanel = seaTempratureGraphPanel;
			System.out.println("Configure: [HarnessFrame -> visual message panel]"
					+ " SeaTempratureSensorMonitor Panel");
		}
		if ("true".equals(prop.getProperty("collectWindSpeed"))) {
			GraphPanel panel = new GraphPanel("Wind Speed");
			visualMsgPanel.add(panel);
			windSpeedGraphPanel = panel;
			if (firstPanel == null)
				firstPanel = windSpeedGraphPanel;
			System.out.println("Configure: [HarnessFrame -> visual message panel]"
					+ " WindSpeedSensorMonitor Panel");
		}
		if ("true".equals(prop.getProperty("collectPosition"))) {
			//JPanel panel = new JPanel();
			Image positionImg = Toolkit.getDefaultToolkit().getImage("position sensor.gif");;
			this.imgPanel.setPositionImg(positionImg);
			//visualMsgPanel.add(panel);
			//windSpeedGraphPanel = panel;
			System.out.println("Configure: [HarnessFrame -> visual message panel]"
					+ " Position Panel");
		}
	}

	public void paint(Graphics g) {
		//g.drawImage(image, 0, 0, this);
		imgPanel.paintComponent(g);
	}

	public boolean handleEvent(Event evt) {
		if (evt.id == Event.WINDOW_DESTROY)
			System.exit(0);
		return super.handleEvent(evt);
	}

	public boolean action(Event evt, Object arg) {
		if (arg.equals("down")) {
			if (sensorValue > 0) {
				sensorField.setText(Integer.toString(--sensorValue));
			}
		} else if (arg.equals("up")) {
			sensorField.setText(Integer.toString(++sensorValue));
		} else {
			return false;
		}
		repaint();
		return true;
	}

	public void displayMessage(int m) {
		if (m < 0)
			numField.setText(Integer.toString(-m));
		else
			msgField.setText(Integer.toString(m));
	}

	Image image;
	TextField sensorField;
	public static int sensorValue = 0; // simulated value for drivers
	TextField msgField;
	TextField numField;
}
